package com.emailsystem.emailsystem.dao.Impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.emailsystem.emailsystem.domain.Email;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Mapper
@Transactional
public interface EmailDAO extends BaseMapper<Email> {

    @Select("select id from tb_email where emailID=#{emailID} limit 1")
    Integer selectExistenceByEmailID(String emailID);

    @Select({
            "<script>",
            "SELECT",
            "*",
            "from tb_email",
            "where emailID in",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            " and deleted=false",
            "</script>"
    })
    ArrayList<Email> selectAllByEmailID(@Param("ids") String[] ids);
    @Insert(
            "insert into tb_email(emailID,sender,receiver,subject,content,send_time,sender_nickname,receiver_nickname,seen,flagged,deleted,attachment_name) values (#{emailID},#{sender},#{receiver},#{subject},#{content},#{sendTime},#{senderNickname},#{receiverNickname},#{seen},#{flagged},#{deleted},#{attachmentName})"
    )
    boolean insertEmail(String emailID, String sender, String receiver, String subject, String content, String sendTime, String senderNickname, String receiverNickname, Boolean seen, Boolean flagged, Boolean deleted,String attachmentName);

    @Update("update tb_email set seen=#{seen} where emailID=#{emailID}")
    void updateSeen(String emailID,Boolean seen);
    @Update("update tb_email set flagged=#{flagged} where emailID=#{emailID}")
    void updateFlagged(String emailID,Boolean flagged);
    @Update("update tb_email set deleted=#{deleted} where emailID=#{emailID}")
    void updateDeleted(String emailID,Boolean deleted);

    @Select("select * from tb_email where receiver=#{account} and deleted=true")
    ArrayList<Email> selectDeleted(String account);
}
